package com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.entity.BooksGoodsCm;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/11/11
 */
@Repository
public class BooksGoodsCmDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String deleteBooksGoodsCmSql = "DELETE FROM sw_books_goods_cm WHERE seqNo=:seqNo and gseqno in (:gseqnos)";

    public void removeBySeqNoAndGseqnoIn(String seqNo, List<String> gseqnos) {
        Map<String, Object> params = new HashMap<>();
        params.put("seqNo", seqNo);
        params.put("gseqnos", gseqnos);
        namedParameterJdbcTemplate.update(this.deleteBooksGoodsCmSql, params);
    }

    private String insertBooksGoodsCmSql = "INSERT INTO sw_books_goods_cm (seqNo, gseqno, cm, storageTime)" +
            " VALUES (:seqNo, :gseqno, :cm, :storageTime)";

    public void save(List<BooksGoodsCm> cmList) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(cmList);
        namedParameterJdbcTemplate.batchUpdate(insertBooksGoodsCmSql, batch);
    }
}
